﻿using HirelSeriesH.Tools;
using MySql.Data.MySqlClient;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HirelSeriesH
{
    public partial class ModifyPasswordForm : UIForm
    {
        public ModifyPasswordForm()
        {
            InitializeComponent();
        }

        private void BtnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {

            string userName = this.TextUserName.Text.Trim();
            string oldPassword = this.TextBoxOldPassword.Text.Trim();
            string password = this.TextPassword.Text.Trim();
            string passwordConfirm = this.TextBoxPasswordConfirm.Text.Trim();
            if (userName == null || userName == "")
            {
                UIMessageDialog.ShowMessageDialog(this, "请输入用户名", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }
            if (oldPassword == null || oldPassword == "")
            {
                UIMessageDialog.ShowMessageDialog(this, "请输入老密码", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }
            if (password == null || password == "")
            {
                UIMessageDialog.ShowMessageDialog(this, "请输入新密码", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }
            //密码6-16位、只能包含英文字母和数字
            var regex = new Regex("^[A-Za-z0-9]{6,16}$", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
            //校验用户名是否符合要求
            bool userNameIsMatch = regex.IsMatch(password);
            if (!userNameIsMatch)
            {
                UIMessageDialog.ShowMessageDialog(this, "密码长度为6-16位，且只能包含英文字母和数字。", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }
            if (passwordConfirm == null || passwordConfirm == "")
            {
                UIMessageDialog.ShowMessageDialog(this, "请输入密码确认", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }
            if (passwordConfirm != password)
            {
                UIMessageDialog.ShowMessageDialog(this, "新密码与密码确认输入不一致", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }

            MySqlParameter[] paras = new MySqlParameter[] { new MySqlParameter("@UserName", userName), new MySqlParameter("@PassWord", oldPassword) };
            object result = DBHelper.ExecuteScalar(DBSql.SELECT_HTRB_USER, 1, paras);
            if(result == null)
            {
                UIMessageDialog.ShowMessageDialog(this, "用户名或者老密码错误，没有查询到相应用户记录。", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }
            string UserID = result.ToString();
            paras = new MySqlParameter[] { new MySqlParameter("@ID", UserID), new MySqlParameter("@PassWord", password) };
            DBHelper.ExecuteNonQuery(DBSql.UPDATE_HTRB_USER_PASSWORD, 1, paras);
            UIMessageDialog.ShowMessageDialog(this, "密码修改成功", UILocalize.InfoTitle, false, UIStyle.Colorful);
            this.Close();
        }
    }
}
